package cn.wolfcode.wolf2w.web.controller;


import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Owner;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.OwnerQuery;
import cn.wolfcode.wolf2w.service.IOwnerService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController // Requestbody + controller
@RequestMapping("/users")
public class UserInfoController {
    //
    @Autowired
    private IUserInfoService userInfoSerivce;//用jar包,相当于是同个项目了.可以用userInfoSerivce
    @Autowired
    private IOwnerService ownerService;


    @Value("${sms.url}")
    private String smsUrl;//获取ap配置文件的数据
    @Value("${sms.appkey}")
    private String smsAppkey;
   //users/get?id=5e295f01a00e265228f963ea

//获取当前登录用户的所有访客对象
    @GetMapping("/get")
    public Object get(String id, @UserParam UserInfo userInfo, OwnerQuery qo){
        Page<Owner> page = ownerService.query(qo);
        return   JsonResult.success( page.getContent());
    }









//检查用户名
    @GetMapping("/checkPhone")
    public Object checkPhone(String phone){
        //where Phone=xx
// userInfo对象有值,说明是已经被人注册了;userInfo对象无值,返回false表示没有没人注册过,可以注册
//        返回false表示可以注册
        boolean result = userInfoSerivce.findByPhone(phone);

        return  !result;//取反,
        // 前端页面逻辑是true,才显示下步登录页面
    }
    //发送验证码
    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone){
        userInfoSerivce.sendVerifyCode(phone);
//调用success方法,返回JsonResult对象,200状态码,信息是成功返回给用户
        return  JsonResult.success();
    }
    //注册
//    calhost:8008/users/regist   users
    @PostMapping("/regist")
    public Object regist(String phone,
      String  nickname,String password,String rpassword,String verifyCode){
            userInfoSerivce.regist( phone,
                     nickname, password, rpassword, verifyCode);
            return  JsonResult.success();
        }
        //登陆操作
    @PostMapping("/login")//POST
    public Object login(String  username,String password){
        UserInfo user = userInfoSerivce.login(username, password);
        //把查询出来的用户对象和token一起存入Redis
        String token = userInfoSerivce.setToken(user);
        Map<String, Object> map = new HashMap<>();
        //key是token;value值:token
        map.put("token",token);
        //key是 user;value值:user
        map.put("user",user);

        return  JsonResult.success(map);
        }

        //登陆后,第二次请求重置时间
    @GetMapping("/currentUser")
    public Object currentUser(HttpServletRequest request){
        //根据名称获取token
        String token = request.getHeader("token");
       UserInfo user= userInfoSerivce.getUserByToken(token);
        return  JsonResult.success(user);
        }



}
